﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WHControlLib.CommonHelpers;

namespace WHControlLib.Controls.Goods
{
    public partial class WHGoodsListPanel : UserControl
    {

        #region 全局参数
        Rectangle LastCheckOutInfoRect = new Rectangle();

        ListCtrlPageManager ItemListManager = new ListCtrlPageManager();

        /// <summary>
        /// 所有要显示的 全部《Goods商品元素》列表 
        /// </summary>
      public  List<Goods> DisplayGoodsItemList = new List<Goods>();


        /// <summary>
        ///  所有添加的《要显示的《商品显示元素控件》列表(最多 GoodsCtrlOnePageCount个)
        /// </summary>
        List<WHGoodsInfo> DisplayGoodsCtrlList = new List<WHGoodsInfo>();


   
      /// <summary>
      /// 所有商品实际付款
      /// </summary>
        public double AllSumGoodsPrice;
        /// <summary>
        /// 所有商品应付款
        /// </summary>
        public double AllSumGoodsReceivablePrice;
        public int AllGoodsSumItemNumber;
       
        /// <summary>
        /// 上单找零
        /// </summary>
        public double GetBackCheckOutMoney;
        public double GetBackLastRealPayMoney;
        public double GetBackLastRecevialblePayMoney;

        int GoodsCtrlOnePageCount;
        int GoodsNowPage = 1;

        int TotleGoodsPage = 1;

        /// <summary>
        /// 这里定义为一行
        /// </summary>

        const int DisplayHCount = 1;

        private int _currentSelectedGoods = -1;
        public int CurrentSelectedGoods
        {
            get { return _currentSelectedGoods; }
            set
            {

                _currentSelectedGoods = value;
            }
        }

        #endregion

        #region 属性定义








        private int _displayVCount = 5;
        [Category("A我的"), Description("控件上显示几列，默认，5列"), Browsable(true)]
        public int DisplayVCount
        {
            get { return _displayVCount; }
            set {
                if (value < 1)
                {
                    _displayVCount = 1;
                }
                else
                    _displayVCount = value;
                GoodsCtrlOnePageCount = _displayVCount ;
            }


        }
        private int _offsetXY = 5;
        [Category("A我的"), Description("各个显示元素之间的空白便宜了，默认，5个像素"), Browsable(true)]
        public int OffsetXY
        {
            get { return _displayVCount; }
            set
            {
                if (value < 0)
                {
                    _offsetXY = 0;
                }
                else
                    _offsetXY = value;
            }


        }


        private bool _isUseTwoColorElement=true;
        [Category("A我的"), Description("显示元素是否启用两种颜色渐变色，默认，true"), Browsable(true)]
        public bool IsUseTwoColorElement
        {
            get { return _isUseTwoColorElement; }
            set { _isUseTwoColorElement = value; }
        }

        private Color _firstElementColor=Color.Bisque;
        [Category("A我的"), Description("显示元素的第一种颜色，默认，Bisque"), Browsable(true)]
        public Color FirstElementColor
        {
            get { return _firstElementColor; }
            set { _firstElementColor = value; }
        }

       
        private Color _secondElementColor = Color.Goldenrod;
        [Category("A我的"), Description("显示元素的第二种颜色，默认，Goldenrod"), Browsable(true)]
        public Color SecondElementColor
        {
            get { return _secondElementColor; }
            set { _secondElementColor = value; }
        }
        private Font _elementFont = new Font("微软雅黑", 12.0f, 0, GraphicsUnit.Point, 1);
        [Category("A我的"), Description(" 控件字体，默认 微软雅黑12t"), Browsable(true)]
        public Font ElementFont
        {
            get { return _elementFont; }
            set { _elementFont = value; }
        }


        private Font _lastOrderInfoFont1 = new Font("微软雅黑", 18.0f, 0, GraphicsUnit.Point, 1);
        [Category("A我的"), Description(" 上单信息字体1，默认 微软雅黑18t"), Browsable(true)]
        public Font LastOrderInfoFont1
        {
            get { return _lastOrderInfoFont1; }
            set { _lastOrderInfoFont1 = value; Invalidate(); }
        }
        private Font _lastOrderInfoFont2 = new Font("微软雅黑", 12.0f, 0, GraphicsUnit.Point, 1);
        [Category("A我的"), Description(" 上单信息字体，默认 微软雅黑12t"), Browsable(true)]
        public Font LastOrderInfoFont2
        {
            get { return _lastOrderInfoFont2; }
            set { _lastOrderInfoFont2 = value; Invalidate(); }


        }


        private Color  _lastOrderInfoFontColor=Color.Black;
        [Category("A我的"), Description(" 上单信息字体颜色，默认 黑色"), Browsable(true)]
        public Color  LastOrderInfoFontColor
        {
            get { return _lastOrderInfoFontColor; }
            set { _lastOrderInfoFontColor = value; }
        }



        private Color _lastOrderInfoFirstackColor=Color.AliceBlue;
        [Category("A我的"), Description(" 上单信息提示框第一种背景色，默认 浅蓝色"), Browsable(true)]
        public Color LastOrderInfoFirstBackColor
        {
            get { return _lastOrderInfoFirstackColor ; }
            set { _lastOrderInfoFirstackColor  = value; Invalidate(); }
        }


        private Color _lastOrderInfoSecondColor = Color.IndianRed;
        [Category("A我的"), Description(" 上单信息提示框第二种背景色，默认 蓝色"), Browsable(true)]
        public Color LastOrderInfoSecondColor
        {
            get { return _lastOrderInfoSecondColor; }
            set { _lastOrderInfoSecondColor = value; Invalidate(); }
        }



        private bool _isElementBorder;
        [Category("A我的"), Description("显示元素是否有边框，默认，false"), Browsable(true)]
        public bool IsElementBorder
        {
            get { return _isElementBorder; }
            set { _isElementBorder = value; Invalidate(); }

        }
        private Color _elementBorderColor=Color.Blue;
        [Category("A我的"), Description("显示元素的边框颜色，默认，蓝色"), Browsable(true)]
        public Color ElementBorderColor
        {
            get { return _elementBorderColor; }
            set { _elementBorderColor = value; Invalidate(); }
        }
        private int _elementBorderWidth=2;
        [Category("A我的"), Description("显示元素的边框的宽度，默认，2"), Browsable(true)]
        public int  ElementBorderWidth
        {
            get { return _elementBorderWidth; }
            set {
                _elementBorderWidth = value; Invalidate();
            }
        }

        private Color _elementFontColor=Color.Black;
        [Category("A我的"), Description("显示元素的字体颜色，默认，黑色"), Browsable(true)]
        public Color ElementFontColor
        {
            get { return _elementFontColor; }
            set { _elementFontColor = value; Invalidate(); }
        }
        private Color _elementSumPriceColor = Color.Red;
        [Category("A我的"), Description("显示元素的当前小计字体颜色，默认，红色"), Browsable(true)]
        public Color ElementSumPriceColor
        {
            get { return _elementSumPriceColor; }
            set { _elementSumPriceColor = value; Invalidate(); }
        }

        private Color _elementFirstSelectedColor=Color.LightGreen;
        [Category("A我的"), Description("显示元素选中时的第一种颜色，默认，浅绿色"), Browsable(true)]
        public Color   ElementFirstSelectedColor
        {
            get { return _elementFirstSelectedColor; }
            set { _elementFirstSelectedColor = value; Invalidate(); }
        }

        private Color _elementSecondSelectedColor = Color.DarkGreen;
        [Category("A我的"), Description("显示元素选中时的第二种颜色，默认 ，深绿色"), Browsable(true)]
        public Color ElementSecondSelectedColor
        {
            get { return _elementSecondSelectedColor; }
            set { _elementSecondSelectedColor = value; Invalidate(); }
        }
      

                private Color _elementDicountFontColor = Color.DarkRed;
        [Category("A我的"), Description("显示元素 优惠提示的颜色，默认 ，深红色"), Browsable(true)]
        public Color ElementDicountFontColor
        {
            get { return _elementDicountFontColor; }
            set { _elementDicountFontColor = value; Invalidate(); }
        }


        private bool _isElementCancombine=true;
        [Category("A我的"), Description("是否添加新元素时候相同元素可以合并 ，默认 true"), Browsable(true)]
        public bool IsElementCancombine
        {
            get { return _isElementCancombine; }
            set { _isElementCancombine = value; }
        }


        public enum Shape
        {
            RoundRectange,
            HalfCircle,
            Rectange
        }
        private Shape _elementShape=Shape.RoundRectange;
        [Category("A我的"), Description("显示元素的外观，默认，圆角矩形"), Browsable(true)]
        public Shape ElementShape
        {
            get { return _elementShape; }
            set { _elementShape = value; }
        }

        private float _elementradius = 5.0f;
        /// <summary>
        /// 圆角大小比例
        /// </summary>
        [Category("A我的"), Description(" 元素圆角大小比例默认5.0,值越大圆角比例越小，不能小于1.1"), Browsable(true)]
        public float ElementRadius
        {
            get { return _elementradius; }
            set
            {
                if (value > 1.1f)
                {
                    _elementradius = value;
                    this.Invalidate();
                }
                else { _elementradius = 1.1f; }

            }
        }

        #endregion




        public WHGoodsListPanel()


        {
            //设置双缓冲开启禁止背景擦拭
            SetStyle(
                     ControlStyles.OptimizedDoubleBuffer
                     | ControlStyles.ResizeRedraw
                     | ControlStyles.Selectable
                     | ControlStyles.AllPaintingInWmPaint
                     | ControlStyles.UserPaint
                     | ControlStyles.SupportsTransparentBackColor,
                     true);


            InitializeComponent();
        }

        // **********************操作过程************************************

        /// <summary>
        /// 得到要显示的自定义（Goods类）对应的《商品显示元素控件》 列表（count在 0 和 GoodsCtrlOnePageCount 之间 动态生成）
        /// </summary>
        /// <param name="GoodsitemList">总商品列表</param>
        /// <param name="GoodsCtrlOnePageCount">每页显示几个商品显示元素控件</param>
        /// <param name="Nowpage">当前要显示的页码号</param>
        /// <returns>这一页要显示的 商品显示元素控件列表 </returns>
        List<WHGoodsInfo>  GetDisplayGoodsCtrlList (List<Goods> GoodsitemList, int GoodsCtrlOnePageCount, int Nowpage)
        {
      
            List<WHGoodsInfo>displaygoodsCtrlList= new List<WHGoodsInfo>();
            List<Goods> TemGoodsList = new List<Goods>();
            TemGoodsList  = ItemListManager.GetSplitedPageList(GoodsitemList, GoodsCtrlOnePageCount,
                                                                   Nowpage, out TotleGoodsPage);
            if (TemGoodsList.Count>0)
            {
              for (int d = 0; d < TemGoodsList .Count; d++)
            {
                double Sum;
                WHGoodsInfo goods = new WHGoodsInfo();
              
                // ————————————————————————元素项 显示出的商品显示信息添加————————————————————
                goods.SerialStr = TemGoodsList [d].Serial.ToString();
              
                goods.GoodsBarcodeStr = TemGoodsList [d].barcode;
                goods.GoodsNameStr = TemGoodsList [d].Name;

                  goods.GoodsNowPriceStr = "单价：￥" + (Math.Round(TemGoodsList[d].Price,2) ).ToString("F2");
                goods.GoodsOldPriceStr=  TemGoodsList [d].OldPrice.ToString("F2");
                goods.ShowGoodsOldPrice = TemGoodsList [d].NeedShowOldPrice ;
                goods.NumberAndUnitStr = TemGoodsList [d].amount.ToString("F2") /*+ " / " */+ TemGoodsList [d].Unit;
           
               
                goods.ShowGoodsDiscountInfo = TemGoodsList[d].NeedShowDiscountInfo;
              
                goods.ShowGoodsGiftInfo = TemGoodsList[d].NeedShowGiftInfo;
                //goods.OtherInfoStr = "无";
                if (goods.ShowGoodsGiftInfo)
                {
                    Sum = 0;
                    goods.GoodsDicountStr = string.Empty;
                }
                  else
                {  
                    goods.GoodsDicountStr = (Math.Round( TemGoodsList[d].Discount,2)*100).ToString()+"折";

                   Sum = TemGoodsList[d].amount *Math.Round(Convert.ToDouble(TemGoodsList[d].Price),2) * Math.Round(Convert.ToDouble(TemGoodsList[d].Discount),2 );
                }
                TemGoodsList[d].SumPrice = Sum;

                goods.GoodsPriceSumStr = "小计：￥" + Sum .ToString("F2");


                //当优惠，打折，改价，赠送后不能合并
                if (goods.ShowGoodsGiftInfo||goods.ShowGoodsOldPrice||goods.ShowGoodsDiscountInfo)
                {
                    TemGoodsList[d].CanAmalgamationItem = false;
                }
                AllSumGoodsPrice = GetGoosSumPriceAndSumAmout(DisplayGoodsItemList, out AllSumGoodsReceivablePrice,out AllGoodsSumItemNumber);

                // ———————————————————————————————————  ——————————————————————————————————————————

                //*******************每项显示样式设置*********************
                goods.IsUseTwoColor = IsUseTwoColorElement;
                goods.FirstFillcolor = FirstElementColor;
                goods.SecondFillcolor= SecondElementColor;
                goods.MyFont = ElementFont;
                goods.IsDrawBoin = IsElementBorder;
                goods.SumPriceColor = ElementSumPriceColor;
                goods.ElementFontColor = ElementFontColor;
                    goods.BorderWidth = ElementBorderWidth;
                goods.BornColor = ElementBorderColor;
                goods.Radius = ElementRadius;
                goods.FirstSelectedColor = ElementFirstSelectedColor;
                goods.SecondSelectedColor = ElementSecondSelectedColor;
                goods.ElementDicountFontColor = ElementDicountFontColor;

            

                switch (ElementShape)
                {
                    case Shape.RoundRectange:
                          goods.MyShape = (baseStaticCtrl.Shape)0;
                        break;

                    case Shape.HalfCircle:
                        goods.MyShape =(baseStaticCtrl.Shape) 1;
                        break;
                    case Shape.Rectange:
                        goods.MyShape = (baseStaticCtrl.Shape)2;
                        break;
                    default:
                        goods.MyShape = (baseStaticCtrl.Shape)0;
                        break;
                }
           
                //***************************************************
                goods.Click += GoodsInfoCtrl_Click;
                displaygoodsCtrlList.Add(goods);

            }
            }
            else
            {
                AllSumGoodsPrice = 0;
                AllSumGoodsReceivablePrice = 0;
                AllGoodsSumItemNumber = 0;
            }
            
            return displaygoodsCtrlList;

        }
        /// <summary>
        /// 点击 《商品显示元素控件》时 使自己为当前是焦点控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GoodsInfoCtrl_Click(object sender, EventArgs e)
        {
            WHGoodsInfo goodInfo = (WHGoodsInfo)sender;

            CurrentSelectedGoods = goodInfo.ThisGoodsIndex - 1;


        }
        /// <summary>
        ///将当前页中的《商品显示元素控件》列表 显示到父控件中
        /// </summary>
        /// <param name="DisplayGoodsItemList">要显示的《Goods商品元素》列表 </param>
        /// <param name="ShowParent">显示在哪呢个父控件</param>
        /// <param name="GoodsCtrlOnePageCount">一页几个控件</param>
        /// <param name="NeedShowPage">要显示的页码号</param>
        /// <param name="CurrentSeledGoodsIndex">当前焦点项的序号</param>
        void ShowNowPage(List<Goods> DisplayGoodsItemList,Control ShowParent,int GoodsCtrlOnePageCount,int NeedShowPage,int CurrentSeledGoodsIndex)

        {
            Rectangle ret = this.ClientRectangle;
            LastCheckOutInfoRect.X = ret.X + OffsetXY;
            LastCheckOutInfoRect.Y = ret.Y + OffsetXY;
            LastCheckOutInfoRect.Width = ret.Width - 2 * OffsetXY;
            LastCheckOutInfoRect.Height = (int)(ret.Height / DisplayVCount);




            DisplayGoodsCtrlList = GetDisplayGoodsCtrlList(DisplayGoodsItemList, GoodsCtrlOnePageCount, NeedShowPage);
            //当没有显示项目时候
            if (CurrentSeledGoodsIndex<0)
            {

                ItemListManager.SetSplitedCtrlListToParent(DisplayGoodsCtrlList, ShowParent, DisplayHCount, DisplayVCount, OffsetXY);

                return;
            }
            //、、当有显示项目时候

            CurrentSelectedGoods = CurrentSeledGoodsIndex;
          

            int  SelectedIndexOfPage= CurrentSeledGoodsIndex / GoodsCtrlOnePageCount + 1;
          
            if (SelectedIndexOfPage==NeedShowPage)
            {
          
            DisplayGoodsCtrlList[CurrentSelectedGoods% GoodsCtrlOnePageCount].IsSelectedMy = true;
            
            }

          

            ItemListManager.SetSplitedCtrlListToParent(DisplayGoodsCtrlList, ShowParent, DisplayHCount, DisplayVCount, OffsetXY);
            this.Invalidate();
       
        }
        //void ShowNowPage(List<Goods> DisplayGoodsItemList, Control ShowParent, int GoodsCtrlOnePageCount, int NeedShowPage)

        //{


        //    DisplayGoodsCtrlList = GetDisplayGoodsCtrlList(DisplayGoodsItemList, GoodsCtrlOnePageCount, NeedShowPage);

        //    ItemListManager.SetSplitedCtrlListToParent(DisplayGoodsCtrlList, ShowParent, DisplayHCount, DisplayVCount, OffsetXY);

        //}
       /// <summary>
       /// 下一页
       /// </summary>
        public void NextPage()
        {
            if (GoodsNowPage < TotleGoodsPage)
            {
                GoodsNowPage++;
            }

            ShowNowPage(DisplayGoodsItemList, this, GoodsCtrlOnePageCount, GoodsNowPage, CurrentSelectedGoods);
        }

       /// <summary>
       /// 上一页
       /// </summary>
        public void UpPage()
        {
            if (GoodsNowPage > 1)
            {
                GoodsNowPage--;
            }

            ShowNowPage(DisplayGoodsItemList, this, GoodsCtrlOnePageCount, GoodsNowPage, CurrentSelectedGoods);
        }
        /// <summary>
        /// 添加要显示的《Goods商品元素》到 显示
        /// </summary>
        /// <param name="goods"></param>
        public void AddToDisplayCtrlList(Goods goods)
        {
            if (goods.amount<=0)
            {goods.amount=1;

            }
            if (goods!=null)
            {
                goods.OldPrice = goods.Price;
                goods.ItemNumber = 1;
            }
            int thisGoodsIndex;


            if (IsElementCancombine &&   goods.IsWeightGoods==false)
            {
                //查询是否有可合并商品
                var d = from x in DisplayGoodsItemList
                        where (x.CanAmalgamationItem == true && x.barcode == goods.barcode)
                        select x;
                if (d.Count() > 0)
                {

                    d.First().amount++;
                    d.First().ItemNumber++;
                    thisGoodsIndex = DisplayGoodsItemList.IndexOf(d.First());
                }

                else
                {
                    DisplayGoodsItemList.Add(goods);


                    thisGoodsIndex = DisplayGoodsItemList.IndexOf(goods);
                    goods.Serial = thisGoodsIndex + 1;

                }



            }
            else
            {
                DisplayGoodsItemList.Add(goods);


                thisGoodsIndex = DisplayGoodsItemList.IndexOf(goods);
                goods.Serial = thisGoodsIndex + 1;

            }


            //将新添加的项 设为焦点项 并获得对应的 所在那个页
            int thisGoodsPage = thisGoodsIndex / GoodsCtrlOnePageCount + 1;
            
            GoodsNowPage= thisGoodsPage;
             CurrentSelectedGoods = thisGoodsIndex;


            ShowNowPage(DisplayGoodsItemList, this, GoodsCtrlOnePageCount, GoodsNowPage, CurrentSelectedGoods);

        }
      /// <summary>
      /// 清空所有商品项
      /// </summary>
        public void ClearAll()
        {
      
            if (DisplayGoodsCtrlList!=null)
            {
                DisplayGoodsCtrlList.Clear();
            }
            if (DisplayGoodsItemList!=null)
                DisplayGoodsItemList.Clear();
            GoodsNowPage = 1;
            TotleGoodsPage = 1;
          CurrentSelectedGoods =-1;
            AllSumGoodsPrice= 0;
            AllSumGoodsReceivablePrice = 0;


             AllGoodsSumItemNumber = 0;

            ShowNowPage(DisplayGoodsItemList, this, GoodsCtrlOnePageCount, GoodsNowPage, CurrentSelectedGoods);

        }
        /// <summary>
        /// 删除一项 有当前焦点项控制 （点击或自动控制焦点项）
        /// </summary>
        public void RemoveFromDisplayCtrl()

        {
            //int thisdelGoodsPage;

            if (  DisplayGoodsCtrlList.Count ==0&&DisplayGoodsCtrlList==null)
            {
                  return;
            }
            if (CurrentSelectedGoods >= 0)
            {

                DisplayGoodsItemList.RemoveAt(CurrentSelectedGoods);

                if (CurrentSelectedGoods < DisplayGoodsItemList.Count)
                {
                    CurrentSelectedGoods++;
                }
                if (CurrentSelectedGoods >= DisplayGoodsItemList.Count)
                {
                    CurrentSelectedGoods--;
                }

                foreach (var item in DisplayGoodsItemList)
                {
                    item.Serial = DisplayGoodsItemList.IndexOf(item) + 1;

                }

                if (CurrentSelectedGoods >= 1 && CurrentSelectedGoods < DisplayGoodsItemList.Count)
                {
                    GoodsNowPage = CurrentSelectedGoods / GoodsCtrlOnePageCount + 1;


                }

                if (CurrentSelectedGoods >= DisplayGoodsItemList.Count)
                {
                    CurrentSelectedGoods = DisplayGoodsItemList.Count;

                    GoodsNowPage = CurrentSelectedGoods / GoodsCtrlOnePageCount + 1;
                }
                ShowNowPage(DisplayGoodsItemList, this, GoodsCtrlOnePageCount, GoodsNowPage,CurrentSelectedGoods);


            }

        }

        /// <summary>
        /// 改变焦点商品的数量为指定数量
        /// </summary>
        /// <param name="NewAmount">指定的数量</param>
       public void ChangeGoodsAmount( double NewAmount)
        {
            if (CurrentSelectedGoods>-1)
            {

                DisplayGoodsItemList[CurrentSelectedGoods].amount = NewAmount;
            ShowNowPage(DisplayGoodsItemList, this, GoodsCtrlOnePageCount, GoodsNowPage, CurrentSelectedGoods);
            }
          

        }
        /// <summary>
        /// 改变焦点商品的价格为指定价格
        /// </summary>
        /// <param name="NewPrice">新价格</param>
        public void ChangeGoodsPrice(double NewPrice)
        {
            if (CurrentSelectedGoods>-1)
            {
                
                DisplayGoodsItemList[CurrentSelectedGoods].NeedShowOldPrice = true;
                DisplayGoodsItemList[CurrentSelectedGoods].Price= NewPrice;

                DisplayGoodsItemList[CurrentSelectedGoods].CanAmalgamationItem = false;

            }
            ShowNowPage(DisplayGoodsItemList, this, GoodsCtrlOnePageCount, GoodsNowPage, CurrentSelectedGoods);
        }
        /// <summary>
        /// 将焦点商品设置为指定折扣（必须是两位小于1的小数，不然要自动截断保留两位）
        /// </summary>
        /// <param name="goodsDiscount">折扣率小于1的小数</param>
        public void SetGoodsDicountInfo(double goodsDiscount )
        {
            if (CurrentSelectedGoods > -1)
            {

                DisplayGoodsItemList[CurrentSelectedGoods].NeedShowDiscountInfo = true;
                DisplayGoodsItemList[CurrentSelectedGoods].Discount= goodsDiscount;
                DisplayGoodsItemList[CurrentSelectedGoods].CanAmalgamationItem = false;
                ShowNowPage(DisplayGoodsItemList, this, GoodsCtrlOnePageCount, GoodsNowPage, CurrentSelectedGoods);
            }

        }
        /// <summary>
        /// 将焦点商品设置为赠送
        /// </summary>
        public void SetGoodsGiftInfo()
        {
            if (CurrentSelectedGoods > -1)
            {
                DisplayGoodsItemList[CurrentSelectedGoods].NeedShowGiftInfo = true;
                DisplayGoodsItemList[CurrentSelectedGoods].CanAmalgamationItem = false;
 
            ShowNowPage(DisplayGoodsItemList, this, GoodsCtrlOnePageCount, GoodsNowPage, CurrentSelectedGoods);
            }
        }
        /// <summary>
        /// j将焦点商品的数量增加一个单位
        /// </summary>
        public void  SetGoodsAmoutAddOne()
        {
            if (CurrentSelectedGoods > -1)
            {
                DisplayGoodsItemList[CurrentSelectedGoods].amount = DisplayGoodsItemList[CurrentSelectedGoods].amount+1;

                ShowNowPage(DisplayGoodsItemList, this, GoodsCtrlOnePageCount, GoodsNowPage, CurrentSelectedGoods);
            }
         }
        /// <summary>
        /// 将焦点商品的数量减少一个单位
        /// </summary>
        public void SetGoodsAmoutDecreaseOne()
        {
            if (CurrentSelectedGoods > -1)
            {
                if (DisplayGoodsItemList[CurrentSelectedGoods].amount>0)
                {

                DisplayGoodsItemList[CurrentSelectedGoods].amount = DisplayGoodsItemList[CurrentSelectedGoods].amount - 1;

                ShowNowPage(DisplayGoodsItemList, this, GoodsCtrlOnePageCount, GoodsNowPage, CurrentSelectedGoods);
                }
               
            }
        }

        private void WHGoodsListPanel_SizeChanged(object sender, EventArgs e)
        {
            ShowNowPage(DisplayGoodsItemList, this, GoodsCtrlOnePageCount, GoodsNowPage, CurrentSelectedGoods);
        }
        /// <summary>
        ///得到商品列表的总价格和总件数
        /// </summary>
        /// <param name="GoodsList">可用商品列表</param>
        /// <param name="SumGoodsAmount">返回总件数</param>
        ///<param name="RecevialbePaymone">返回应付款</param>
        /// <returns><实付款/returns>

        double GetGoosSumPriceAndSumAmout(List<Goods> GoodsList,out double RecevialbePaymone, out int SumGoodsitemNumber)
        {
            double sumprice = 0;
            SumGoodsitemNumber = 0;
            foreach (var item in GoodsList)
            {
                sumprice += item.SumPrice;
                SumGoodsitemNumber += item.ItemNumber;
            }
           RecevialbePaymone = 0;
            foreach (var item in GoodsList)
            {

                double d = item.amount * item.OldPrice;
                RecevialbePaymone += d;


            }
            RecevialbePaymone = Math.Round(RecevialbePaymone, 2);
            return Math.Round(sumprice, 2);
        
        }
        /// <summary>
        /// 显示上单付款信息
        /// </summary>
        /// <param name="g"></param>
        /// <param name="receiptsMoney">实际收入</param>
        /// <param name="receivableMoney">应收</param>
        /// <param name="NeedPayMoney">上单找零</param>
        void DrawLastCheckOutInfo(Graphics g,double receiptsMoney,double receivableMoney,double NeedPayMoney)
        {
            if (IsUseTwoColorElement)
            {
                using (LinearGradientBrush lbrsh=new  LinearGradientBrush (LastCheckOutInfoRect,LastOrderInfoFirstBackColor,LastOrderInfoSecondColor,LinearGradientMode.Vertical))
                {
                    g.FillPath(lbrsh, DrawHelper.GetRoundRectangePath(LastCheckOutInfoRect, 0, 5));
                }



            }
            else
            {
                      using (SolidBrush sb = new SolidBrush(LastOrderInfoFirstBackColor))
                      {
                       g.FillPath( sb, DrawHelper.GetRoundRectangePath(LastCheckOutInfoRect, 0, 5));
                       }
            }
         


            int offset=5;
            Rectangle NeedPayRect = new Rectangle();
            NeedPayRect.X = LastCheckOutInfoRect.X + offset;
            NeedPayRect.Y = LastCheckOutInfoRect.Y + offset;
            NeedPayRect.Width = LastCheckOutInfoRect.Width / 2 - offset;
            NeedPayRect.Height = (int)(LastCheckOutInfoRect.Height / 3 * 2);
            
            Rectangle NeedPayMoneyRect = new Rectangle();
            NeedPayMoneyRect.X = NeedPayRect.X + NeedPayRect.Width;
            NeedPayMoneyRect.Y = NeedPayRect.Y;
            NeedPayMoneyRect.Width = NeedPayRect.Width;
            NeedPayMoneyRect.Height = NeedPayRect.Height;
          
            Rectangle receivableRect = new Rectangle();
            receivableRect.X = NeedPayRect.X;
            receivableRect.Y = NeedPayRect.X + NeedPayRect.Height;
            receivableRect.Width = LastCheckOutInfoRect.Width - 2 * offset;
            receivableRect.Height=LastCheckOutInfoRect.Height-NeedPayRect.Height- 2 * offset;
            
            StringFormat sfLeft = new StringFormat();
            sfLeft.Alignment = StringAlignment.Near;
            sfLeft.LineAlignment = StringAlignment.Center;
            StringFormat sfRight = new StringFormat();
            sfRight.Alignment = StringAlignment.Far;
            sfRight.LineAlignment = StringAlignment.Center;
            
           
            using (SolidBrush sb=new SolidBrush(LastOrderInfoFontColor))
            {
                g.DrawString("上单找零：", LastOrderInfoFont1, sb, NeedPayRect, sfLeft);
                g.DrawString(NeedPayMoney.ToString("F2"), LastOrderInfoFont1, sb, NeedPayMoneyRect, sfRight);
                g.DrawString("应收金额："+receivableMoney.ToString("F2"), LastOrderInfoFont2, sb, receivableRect, sfLeft);
                g.DrawString("实收金额：" + receiptsMoney.ToString("F2"), LastOrderInfoFont2, sb, receivableRect, sfRight);

            }



            if (IsElementBorder)
            {
                using (Pen pen=new Pen(ElementBorderColor,ElementBorderWidth))
                {
                    g.DrawPath(pen, DrawHelper.GetRoundRectangePath(LastCheckOutInfoRect, 0, 5));
                }
            }

           


        }

       /// <summary>
       /// 获得上单结账后的回传信息
       /// </summary>
       /// <param name="LastRealPayMoney"></param>
       /// <param name="LastRecevialblePayMoney"></param>
       /// <param name="LastCheckOutMoney"></param>
        public void GetLastOrderInfo(double LastRealPayMoney,double LastRecevialblePayMoney,double LastCheckOutMoney)

        {

            GetBackLastRealPayMoney = LastRealPayMoney;
            GetBackLastRecevialblePayMoney = LastRecevialblePayMoney;
            GetBackCheckOutMoney = LastCheckOutMoney;


        }
            protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (DisplayGoodsItemList.Count<1)
            {
              DrawLastCheckOutInfo(e.Graphics, GetBackLastRealPayMoney, GetBackLastRecevialblePayMoney, GetBackCheckOutMoney);
          
            }else
                using (SolidBrush sb=new SolidBrush(BackColor))
                {
                    e.Graphics.FillRectangle(sb, this.ClientRectangle);
                }
            
            
        }

        //************************************************************


    }
    public  class Goods
        {
            public int Serial = 0;

            public string Name="";
            public string barcode="";
            public double Price = 0;
            public double OldPrice = 0;
            public double SumPrice = 0;
            public double Discount =1;
            public string Unit="";
            public double amount=0;
         
            public string ClassId="";
            public string OtherMsg="";
        public bool NeedShowDiscountInfo = false;
        public bool NeedShowOldPrice=false;
        public bool NeedShowGiftInfo = false;
        public bool IsWeightGoods = false;
        public int ItemNumber = 0;
        /// <summary>
        /// 是否可以合并项目，当优惠，打折，改价，赠送后不能合并
        /// </summary>
        public bool CanAmalgamationItem = true;
        }

     
 
}
